/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.cloud.sys.modules.sys.controller;

import com.cloud.common.pojo.sys.SysUserEntity;
import com.cloud.common.utils.Constant;
import com.cloud.common.utils.PageUtils;
import com.cloud.common.utils.R;
import com.cloud.common.validator.Assert;
import com.cloud.common.validator.ValidatorUtils;
import com.cloud.common.validator.group.AddGroup;
import com.cloud.common.validator.group.UpdateGroup;
import com.cloud.sys.modules.sys.form.PasswordForm;
import com.cloud.sys.modules.sys.service.SysUserRoleService;
import com.cloud.sys.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/user")
@Api(value = "用户表", tags = {"用户表相关接口"})
public class SysUserController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;


	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
	public R list(@RequestHeader("userId") long userId, @RequestParam Map<String, Object> params) {
		//只有超级管理员，才能查看所有管理员列表
		if (userId != Constant.SUPER_ADMIN) {
			params.put("createUserId", userId);
		}
		PageUtils page = sysUserService.queryPage(params);

		return R.ok().put("page", page);
	}
	
	/**
	 * 获取登录的用户信息  @RequestHeader("userId") long userId
	 */
	@GetMapping("/info")
	public R info(@RequestHeader("userId") long userId) {
		SysUserEntity sysUserEntity=sysUserService.getById(userId);
		return R.ok().put("user", sysUserEntity);
	}
	
	/**
	 * 修改登录用户密码
	 */
//	@SysLog("修改密码")
	@PostMapping("/password")
	public R password(@RequestHeader("userId") long userId, @RequestBody PasswordForm form) {
		Assert.isBlank(form.getNewPassword(), "新密码不为能空");

		SysUserEntity userEntity = sysUserService.getById(userId);
		
		//sha256加密
		String password = new Sha256Hash(form.getPassword(), userEntity.getSalt()).toHex();
		//sha256加密
		String newPassword = new Sha256Hash(form.getNewPassword(), userEntity.getSalt()).toHex();
				
		//更新密码
		boolean flag = sysUserService.updatePassword(userId, password, newPassword);
		if(!flag){
			return R.error("原密码不正确");
		}
		
		return R.ok();
	}
	
	/**
	 * 用户信息
	 */
//	@GetMapping("/info/{userId}")
//	public R info(@PathVariable("userId") Long userId){
//		SysUserEntity user = sysUserService.getById(userId);
//
//		//获取用户所属的角色列表
//		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
//		user.setRoleIdList(roleIdList);
//
//		return R.ok().put("user", user);
//	}
//
	/**
	 * 保存用户
	 */
//	@SysLog("保存用户")
	@PostMapping("/save")
	public R save(@RequestHeader("userId") long userId, @RequestBody SysUserEntity user) {
//		ValidatorUtils.validateEntity(user, AddGroup.class);
		user.setCreateUserId(userId);
		sysUserService.saveUser(user);
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
//	@SysLog("修改用户")
	@PostMapping("/update")
	public R update(@RequestHeader("userId") long userId, @RequestBody SysUserEntity user) {
//		ValidatorUtils.validateEntity(user, UpdateGroup.class);

		user.setCreateUserId(userId);
		sysUserService.update(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
//	@SysLog("删除用户")
	@PostMapping("/delete")
	public R delete(@RequestHeader("userId") long userId, @RequestBody Long[] userIds) {
		if(ArrayUtils.contains(userIds, 1L)){
			return R.error("系统管理员不能删除");
		}

		if (ArrayUtils.contains(userIds, userId)) {
			return R.error("当前用户不能删除");
		}
		
		sysUserService.deleteBatch(userIds);
		
		return R.ok();
	}
}
